# -*- coding:utf-8 -*-
# created on 2017/3/1
#

from mathsolver.functions.base import *
from mathsolver.functions.base.base import new_latex


# (a+x)(1+x)^{4}的展开式中x的奇数次幂项的系数之和为32,则a=().
class EXSJiShuFuZhiFa(BaseFunction):
    def solver(self, *args):
        poly = args[0].sympify()
        self.steps.append(["", "设f(x) = %s, 则" % (new_latex(poly))])
        symbol = default_symbol(poly)
        f1 = poly
        f1 = f1.subs({symbol: 1})
        f2 = poly
        f2 = f2.subs({symbol: -1})
        self.steps.append(["", "令%s = %s, 得 f(1) = %s" % (new_latex(symbol), new_latex(1), new_latex(f1))])
        self.steps.append(["", "令%s = %s, 得 f(1) = %s" % (new_latex(symbol), new_latex(-1), new_latex(f2))])
        jishu_sum = (f1 - f2) / 2
        self.steps.append(["", "所以, %s的奇数次幂项的系数之和为: %s" % (new_latex(poly), new_latex(jishu_sum))])
        self.output.append(BaseNumber(jishu_sum))
        self.label.add("求二项式的奇数项系数和")
        return self
